package com.host.dyyl.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;

import android.view.MotionEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.host.dyyl.R;


/**
 * @author
 * @describe
 * @fileName ProgressWebview
 * @date 2017/12/16 22:47
 */

public class ProgressWebView extends WebView {

    private Context mContext;
    private ProgressBar progressbar;

    private final static int FILECHOOSER_RESULTCODE = 1;
    public ValueCallback<Uri> mUploadMessage;
    public ValueCallback<Uri[]> mUploadCallbackAboveL;

    public ProgressWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        progressbar = new ProgressBar(context, null,
                android.R.attr.progressBarStyleHorizontal);
        progressbar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                6, 0, 0));

        Drawable drawable = context.getResources().getDrawable(R.drawable.progressbar_style);
        progressbar.setProgressDrawable(drawable);
        addView(progressbar);
        // setWebViewClient(new WebViewClient(){});
        setWebChromeClient(new MyWebChromeClient());
        //是否可以缩放
        initWebViewSetting();
    }

    private void initWebViewSetting() {
        WebSettings webSetting = this.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setAllowFileAccess(true);
//		webSetting.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
//        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        //webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
//        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSetting.setCacheMode(WebSettings.LOAD_DEFAULT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webSetting.setBlockNetworkImage(false);
    }

    public class MyWebChromeClient extends WebChromeClient {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressbar.setVisibility(GONE);
                if (mOnRefreshFinishListener != null) {
                    mOnRefreshFinishListener.finish();
                }
            } else {
                if (progressbar.getVisibility() == GONE)
                    progressbar.setVisibility(VISIBLE);
                progressbar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }


        @Override
        public void onReceivedTitle(WebView webView, String s) {
            super.onReceivedTitle(webView, s);
            if (!TextUtils.isEmpty(s)) {
//                    title_tv.setText(s);
                if (mOnRefreshTitleListener != null) {
                    mOnRefreshTitleListener.refresh(s);
                }
            }
        }

    }

    public interface OnRefreshTitleListener {
        void refresh(String title);
    }

    private OnRefreshTitleListener mOnRefreshTitleListener;

    public void setOnRefreshTitleListener(OnRefreshTitleListener onRefreshTitleListener) {
        this.mOnRefreshTitleListener = onRefreshTitleListener;
    }

    public interface OnRefreshFinishListener {
        void finish();
    }

    private OnRefreshFinishListener mOnRefreshFinishListener;



    //
//    @Override
//    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
//        LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();
//        lp.x = l;
//        lp.y = t;
//        progressbar.setLayoutParams(lp);
//        super.onScrollChanged(l, t, oldl, oldt);
//    }

//    @Override
//    public void onScrollChanged(int l, int t, int oldl, int oldt) {
//        super.onScrollChanged(l, t, oldl, oldt);
//        if (onRefreshAbleListener != null) {
//            onRefreshAbleListener.scroll(this.getScrollY() == 0);
//        }
//    }
//
//    public interface OnRefreshAbleListener {
//        void scroll(boolean b);
//    }
//
//    private OnRefreshAbleListener onRefreshAbleListener;
//
//    public void setOnRefreshAbleListener(OnRefreshAbleListener onRefreshAbleListener) {
//        this.onRefreshAbleListener = onRefreshAbleListener;
//    }
}
